﻿Imports System.IO
Imports System.ServiceModel
Imports System.ServiceProcess

Class EnterPassword
    Implements IPopup

#Region "Constructors"
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        StartWCFService()
        CreateTrayIcon()
        Using service As ServiceController = New ServiceController("KeyMonitor")
            If (service.Status = ServiceControllerStatus.Stopped) Then                
                service.Start()
                _splash = New StartService()
                _splash.Show()
                _splash.Focus()
                _splash.Refresh()
                Try
                    service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(15))
                Catch
                    If service.Status = ServiceControllerStatus.Stopped Then
                        MessageBox.Show("Unable to start KeyMonitor windows service. Try to reinstall application.", "Error", MessageBoxButton.OK, MessageBoxImage.Error)
                        Me.Close()
                        Return
                    End If
                End Try
                _splash.Close()
            End If
        End Using        

    End Sub
#End Region

#Region "Public Properties"
    Public Property PinCode() As String
        Get
            Return _pinCode
        End Get
        Set(ByVal Value As String)
            _pinCode = Value
        End Set
    End Property
#End Region

#Region "UI Handlers"

    Private Sub UserControl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles btnClose.MouseDown
        If MessageBox.Show("Do you really want to close it?", "Abort", MessageBoxButton.OKCancel, MessageBoxImage.Asterisk) = MessageBoxResult.OK Then
            Me.Close()
        End If
    End Sub

    Private Sub btn1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn1.Click
        VerifyPinCode(1)
    End Sub

    Private Sub btn2_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn2.Click
        VerifyPinCode(2)
    End Sub

    Private Sub btn3_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn3.Click
        VerifyPinCode(3)
    End Sub

    Private Sub btn4_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn4.Click
        VerifyPinCode(4)
    End Sub

    Private Sub btn5_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn5.Click
        VerifyPinCode(5)
    End Sub

    Private Sub btn6_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn6.Click
        VerifyPinCode(6)
    End Sub

    Private Sub btn7_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn7.Click
        VerifyPinCode(7)
    End Sub

    Private Sub btn8_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn8.Click
        VerifyPinCode(8)
    End Sub

    Private Sub btn9_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn9.Click
        VerifyPinCode(9)
    End Sub

    Private Sub m_notifyIcon_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If (e.Button = Forms.MouseButtons.Right) Then
            Dim menu As ContextMenu = CType(Me.FindResource("NotifierContextMenu"), ContextMenu)
            menu.IsOpen = True
        End If
    End Sub

    Private Sub EnterPassword_Closing(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closing
        _notifyIcon.Dispose()
        _notifyIcon = Nothing
        _service.Close()
    End Sub

    Private Sub ShowSettings(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If ValidateAccess() Then
            If _settings Is Nothing Then
                _settings = New Settings()
                _settings.ShowDialog()
                _settings = Nothing
            End If
        End If
    End Sub

    Private Sub ExitApp(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If ValidateAccess() Then
            Using service As ServiceController = New ServiceController("KeyMonitor")
                If (service.Status = ServiceControllerStatus.Running) Then
                    service.Stop()
                End If
            End Using
            Me.Close()
        End If
    End Sub
#End Region

#Region "Public Methods"

    Public Function GetStatus() As Boolean Implements IPopup.GetStatus
        Return String.Compare(PinCode, My.Settings.PinCode, False) = 0
    End Function

    Public Sub ShowPopup() Implements IPopup.ShowPopup
        PinCode = String.Empty
        Me.Show()
    End Sub

#End Region

#Region "Helper Methods"
    Private Sub VerifyPinCode(ByVal n As Integer)
        If PinCode.Length = 4 Then
            PinCode = PinCode.Remove(0, 1)
        End If
        PinCode = String.Concat(PinCode, n.ToString())
        If String.Compare(PinCode, My.Settings.PinCode, False) = 0 Then
            Me.Visibility = Windows.Visibility.Hidden
        End If
    End Sub

    Private Function ValidateAccess() As Boolean
        If _pass Is Nothing Then
            Dim result As Boolean
            _pass = New ValidateAccess()
            _pass.ShowDialog()
            result = _pass.AuthicationPassed
            _pass = Nothing
            Return result
        Else
            Return False
        End If
    End Function

    Private Sub StartWCFService()
        Dim manager As New LockManager(Me)
        Dim baseAddress As New Uri("net.tcp://localhost:9000/LockManager")
        _service = New ServiceHost(manager, baseAddress)
        _service.Open()
    End Sub

    Private Sub CreateTrayIcon()
        Me.Visibility = Windows.Visibility.Hidden
        _notifyIcon = New System.Windows.Forms.NotifyIcon()
        _notifyIcon.BalloonTipText = "The Password Safe has been minimised. Click the tray icon to show."
        _notifyIcon.BalloonTipTitle = "Password Safe"
        _notifyIcon.Text = "Password Safe"
        Dim iconStream As Stream = Application.GetResourceStream(New Uri("pack://application:,,,/Terminator;component/Resources/Icons/lock.ico")).Stream
        _notifyIcon.Icon = New System.Drawing.Icon(iconStream)
        AddHandler _notifyIcon.MouseDown, AddressOf m_notifyIcon_MouseDown
        _notifyIcon.Visible = True
    End Sub
#End Region

#Region "Private Fields"
    Private _notifyIcon As System.Windows.Forms.NotifyIcon
    Private _pinCode As String = String.Empty
    Private _service As ServiceHost
    Private _pass As ValidateAccess
    Private _settings As Settings
    Private _splash As StartService
#End Region

End Class
